$(function () {
    var seleteDoctorValue = $('#userName').val();
    if (seleteDoctorValue != "") {
        $("#doctor1 option[value='" + seleteDoctorValue + "']").attr("selected", "selected");
    }
    var seleteStoreValue = $('#storeValue').val();
    if (seleteStoreValue != "") {
        $("#arrivalStore option[value='" + seleteStoreValue + "']").attr("selected", "selected");
    }
    cancel.onclick = function () {
        parent.layer.close(parent.layer.getFrameIndex(window.name));
    };
    machiningNumbers = function (obj) {
        $(obj).val($(obj).val().replace(/[^\d]/g, ''));
        var price = $(obj).parent().siblings().children('.price').val();
        var amount1 = parseInt(price) * parseInt($(obj).val());
        if (isNaN(amount1) != true) {
            $(obj).parent().siblings().children('.amount1').val(amount1);
            $(obj).parent().siblings().children('.amountActually1').val(amount1)
        }
    };
    maniningPrices = function (obj) {
        $(obj).val($(obj).val().replace(/[^\d]/g, ''));
        var num = $(obj).parent().siblings().children('.number1').val();
        var amount1 = parseInt(num) * parseInt($(obj).val());
        if (isNaN(amount1) != true) {
            $(obj).parent().siblings().children('.amount1').val(amount1);
            $(obj).parent().siblings().children('.amountActually1').val(amount1)
        }
    };
    ensure1.onclick = function () {
        var leftUp = $(".leftUp1").val();
        var rightUp = $(".rightUp1").val();
        var rightDown = $(".rightDown1").val();
        var leftDown = $(".leftDown1").val();
        var payServiceName = $(".payServiceName").val();
        var arrivalStore = $(".arrivalStore2").val();
        var number = $(".number1").val();
        var amount = $(".amount1").val();
        var amountActually = $(".amountActually1").val();
        var price = $(".price").val();
        var type = $(".type1").val();
        var processUnit = $("#processUnit").val();
        var status = $("#status").val();
        var doctor = $("#doctor1").val();
        var remarks = $("#remarks1").val();
        var id = $("#id").val();
        if ($(".payServiceName").val() == "" || $(".payServiceName").val() == null) {
            alert("耗材不能为空");
            return;
        }
        if ($(".number1").val() == "" || $(".number1").val() == null) {
            alert("数量类别不能为空");
            return;
        }
        if ($(".amount1").val() == "" || $(".amount1").val() == null) {
            alert("金额不能为空");
            return;
        }
        if ($(".amountActually1").val() == "" || $(".amountActually1").val() == null) {
            alert("实付金额不能为空");
            return;
        }
        if ($(".type1").val() == "" || $(".type1").val() == null) {
            alert("类别不能为空");
            return;
        }
        $.ajax({
            url: Feng.ctxPath + "/doctorLooked/update_technology",
            type: "post",
            data: {
                doctor: doctor,
                leftUp: leftUp,
                rightUp: rightUp,
                remarks: remarks,
                rightDown: rightDown,
                arrivalStore: arrivalStore,
                leftDown: leftDown,
                payServiceName: payServiceName,
                number: number,
                price: price,
                type: type,
                processUnit: processUnit,
                status: status,
                id: id,
                amount: amount,
                amountActually: amountActually
            },
            success: function (data) {
                Feng.success('操作成功!');
                window.parent.TechnologyProcessingTableList.table.refresh();
                $("#cancel").click();
            },
            error: function (data) {
                alert('没有权限，请联系管理员!');
            }
        });
    };
    var index;
    $(".machiningBox").on('click', '.toothBox', function (e) {
        e.stopPropagation();
        $(".toothBox").removeClass('tips');
        $(this).addClass('tips');
        var leftUpNum = $(this).find('.leftUp1').val();
        var rightUpNum = $(this).find('.rightUp1').val();
        var leftDownNum = $(this).find('.leftDown1').val();
        var rightDownNum = $(this).find('.rightDown1').val();
        index = layer.open({
            type: 4,
            tips: [1, '#ffffff'],
            closeBtn: 0,
            area: ['auto', 'auto'],
            content: ['<div>' + $("#noneBox").html() + '</div>', '.tips'],
            success: function (layero, index) {
                if (leftUpNum.length > 0) {
                    for (var w = 0; w < $(layero).find('.toothTopLeft>span').length; w++) {
                        for (var i = 0; i < leftUpNum.length; i++) {
                            if ($(layero).find('.toothTopLeft>span').eq(w).find('span').text() == leftUpNum.substring(i, i + 1)) {
                                $(layero).find('.toothTopLeft>span').eq(w).find('span').addClass('spanStyle');
                            }
                        }
                    }
                }

                if (rightUpNum.length > 0) {
                    for (var w = 0; w < $(layero).find('.toothTopRight>span').length; w++) {
                        for (var i = 0; i < rightUpNum.length; i++) {
                            if ($(layero).find('.toothTopRight>span').eq(w).find('span').text() == rightUpNum.substring(i, i + 1)) {
                                $(layero).find('.toothTopRight>span').eq(w).find('span').addClass('spanStyle');
                            }
                        }
                    }
                }

                if (leftDownNum.length > 0) {
                    for (var w = 0; w < $(layero).find('.toothBottomLeft>span').length; w++) {
                        for (var i = 0; i < leftDownNum.length; i++) {
                            if ($(layero).find('.toothBottomLeft>span').eq(w).find('span').text() == leftDownNum.substring(i, i + 1)) {
                                $(layero).find('.toothBottomLeft>span').eq(w).find('span').addClass('spanStyle');
                            }
                        }
                    }
                }

                if (rightDownNum.length > 0) {
                    for (var w = 0; w < $(layero).find('.toothBottomRight>span').length; w++) {
                        for (var i = 0; i < rightDownNum.length; i++) {
                            if ($(layero).find('.toothBottomRight>span').eq(w).find('span').text() == rightDownNum.substring(i, i + 1)) {
                                $(layero).find('.toothBottomRight>span').eq(w).find('span').addClass('spanStyle');
                            }
                        }
                    }
                }
            }
        });
    });
    $(document).on('mouseleave', '#tipsBox', function () {
        layer.close(index)
    });
    $(document).on('click', '.tipsUl>li', function (e) {
        e.stopPropagation();
        $(this).addClass('tipsLi').siblings().removeClass('tipsLi');
        if ($(this).index() == 0) {
            $(this).parent().siblings().children('#permanent').show().siblings().hide()
        } else if ($(this).index() == 1) {
            $(this).parent().siblings().children('#deciduous').show().siblings().hide()
        }
    });
    $(document).on('click', '.toothTopLeft>span', function (e) {
        e.stopPropagation();
        var arr = [];
        var text = $(this).text();
        if ($(this).children().attr('class') == 'spanStyle') {
            var inputValue = $(".tips .leftUp1").val().replace(text, "");
            for (var i = 0; i < inputValue.length; i++) {
                arr.push(inputValue.substring(i, i + 1));
            }
            for (var i = arr.length - 1; i >= 0; i--) {
                for (var j = 0; j < i; j++) {
                    if (arr[j + 1] > arr[j]) {
                        var change = arr[j + 1];
                        arr[j + 1] = arr[j];
                        arr[j] = change
                    }
                }
            }
            var str = "";
            for (var i = 0; i < arr.length; i++) {
                str += arr[i]
            }
            $(".tips .leftUp1").val(str);
            $(this).children().removeClass('spanStyle');
        } else {
            var add = $(".tips .leftUp1").val() + text;
            for (var d = 0; d < add.length; d++) {
                arr.push(add.substring(d, d + 1));
            }
            for (var i = arr.length - 1; i >= 0; i--) {
                for (var j = 0; j < i; j++) {
                    if (arr[j + 1] > arr[j]) {
                        var change = arr[j + 1];
                        arr[j + 1] = arr[j];
                        arr[j] = change
                    }
                }
            }
            var str = "";
            for (var i = 0; i < arr.length; i++) {
                str += arr[i]
            }
            $(this).children().addClass('spanStyle');
            $(".tips .leftUp1").val(str)
        }
    });
    $(document).on('click', '.toothTopRight>span', function (e) {
        e.stopPropagation();
        var arr = [];
        var text = $(this).text();
        if ($(this).children().attr('class') == 'spanStyle') {
            var inputValue = $(".tips .rightUp1").val().replace(text, "");
            for (var i = 0; i < inputValue.length; i++) {
                arr.push(inputValue.substring(i, i + 1));
            }
            for (var i = arr.length - 1; i >= 0; i--) {
                for (var j = 0; j < i; j++) {
                    if (arr[j + 1] < arr[j]) {
                        var change = arr[j + 1];
                        arr[j + 1] = arr[j];
                        arr[j] = change
                    }
                }
            }
            var str = "";
            for (var i = 0; i < arr.length; i++) {
                str += arr[i]
            }
            $(".tips .rightUp1").val(str);
            $(this).children().removeClass('spanStyle');
        } else {
            $(this).children().addClass('spanStyle');
            var add = $(".tips .rightUp1").val() + text;
            for (var d = 0; d < add.length; d++) {
                arr.push(add.substring(d, d + 1));
            }
            for (var i = arr.length - 1; i >= 0; i--) {
                for (var j = 0; j < i; j++) {
                    if (arr[j + 1] < arr[j]) {
                        var change = arr[j + 1];
                        arr[j + 1] = arr[j];
                        arr[j] = change
                    }
                }
            }
            var str = "";
            for (var i = 0; i < arr.length; i++) {
                str += arr[i]
            }
            $(".tips .rightUp1").val(str)
        }
    });
    $(document).on('click', '.toothBottomLeft>span', function (e) {
        e.stopPropagation();
        var arr = [];
        var text = $(this).text();
        if ($(this).children().attr('class') == 'spanStyle') {
            var inputValue = $(".tips .leftDown1").val().replace(text, "");
            for (var i = 0; i < inputValue.length; i++) {
                arr.push(inputValue.substring(i, i + 1));
            }
            for (var i = arr.length - 1; i >= 0; i--) {
                for (var j = 0; j < i; j++) {
                    if (arr[j + 1] > arr[j]) {
                        var change = arr[j + 1];
                        arr[j + 1] = arr[j];
                        arr[j] = change
                    }
                }
            }
            var str = "";
            for (var i = 0; i < arr.length; i++) {
                str += arr[i]
            }
            $(".tips .leftDown1").val(str);
            $(this).children().removeClass('spanStyle');
        } else {
            $(this).children().addClass('spanStyle');
            var add = $(".tips .leftDown1").val() + text;
            for (var d = 0; d < add.length; d++) {
                arr.push(add.substring(d, d + 1));
            }
            for (var i = arr.length - 1; i >= 0; i--) {
                for (var j = 0; j < i; j++) {
                    if (arr[j + 1] > arr[j]) {
                        var change = arr[j + 1];
                        arr[j + 1] = arr[j];
                        arr[j] = change
                    }
                }
            }
            var str = "";
            for (var i = 0; i < arr.length; i++) {
                str += arr[i]
            }
            $(".tips .leftDown1").val(str)
        }
    });
    $(document).on('click', '.toothBottomRight>span', function (e) {
        e.stopPropagation();
        var arr = [];
        var text = $(this).text();
        if ($(this).children().attr('class') == 'spanStyle') {
            var inputValue = $(".tips .rightDown1").val().replace(text, "");
            for (var i = 0; i < inputValue.length; i++) {
                arr.push(inputValue.substring(i, i + 1));
            }
            for (var i = arr.length - 1; i >= 0; i--) {
                for (var j = 0; j < i; j++) {
                    if (arr[j + 1] < arr[j]) {
                        var change = arr[j + 1];
                        arr[j + 1] = arr[j];
                        arr[j] = change
                    }
                }
            }
            var str = "";
            for (var i = 0; i < arr.length; i++) {
                str += arr[i]
            }
            $(".tips .rightDown1").val(str);
            $(this).children().removeClass('spanStyle');
        } else {
            $(this).children().addClass('spanStyle');
            var add = $(".tips .rightDown1").val() + text;
            for (var d = 0; d < add.length; d++) {
                arr.push(add.substring(d, d + 1));
            }
            for (var i = arr.length - 1; i >= 0; i--) {
                for (var j = 0; j < i; j++) {
                    if (arr[j + 1] < arr[j]) {
                        var change = arr[j + 1];
                        arr[j + 1] = arr[j];
                        arr[j] = change
                    }
                }
            }
            var str = "";
            for (var i = 0; i < arr.length; i++) {
                str += arr[i]
            }
            $(".tips .rightDown1").val(str)
        }
    });
});